package com.example.demo.service.impl;

import com.example.demo.mapepr.second.UserMapper;
import com.example.demo.pojo.second.User;
import com.example.demo.service.UserService;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * cacheNames:缓存名字  一个CacheManager对应多个Cache,CacheManager根据名字管理Cache,缓存的名字是一个数组，也就是说可以将一个缓存
     * * Entry分到多个Cache里面
     * key: Entry的key
     *
     * @param id id
     * @return
     */
    @Override
    @Cacheable(cacheNames = "user", key = "#id", condition = "#id > 0", unless = "#result == null")
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    @Transactional
    @CachePut(cacheNames = "user", key = "#user.id", condition = "#user.id > 0", unless = "#result == null")
    public User updateUser(User user) {
        userMapper.updateUser(user);
        return user;
    }
}
